<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/2/7
 * Time: 15:59
 */

namespace app\index\controller;


use app\index\validate\Valid;

class Ggxx extends Common
{
    //列表页
    public function index(){
        $title = $this->request->get('title');
        $this->assign('title',$title);
        $lx = $this->request->get('lx');
        $this->assign('lx',$lx);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        $where = [];
        if($title){
            $where['title'] = ['like',"%{$title}%"];
        }
        if($lx){
            $where['lx'] = $lx;
        }
        if($start&&!$end){
            $where['fbrq'] = ['>',$start];
        }elseif($end&&!$start){
            $where['fbrq'] = ['<',$end];
        }elseif($start&&$end){
            $where['fbrq'] = [['>',$start],['<',$end],'and'];
        }
        $count = db('cg_ggxx')->where($where)->count();
        $data = db('cg_ggxx')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['lxmc'] = db('cg_ggxx_lx')->where('id',$v['lx'])->value('title');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        $lxs = db('cg_ggxx_lx')->select();
        $this->assign('lxs',$lxs);

        return $this->fetch();
    }

    //详情页
    public function view(){
        $id = $this->request->param('id');
        $data = db('cg_ggxx')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    //新闻公告管理
    public function newsbulletinma(){
        $where = [];
        $count = db('cg_ggxx')->where($where)->count();
        $data = db('cg_ggxx')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['lx'] = db('cg_ggxx_lx')->where('id',$v['lx'])->value('title');
            $v['sd_sfzd'] = $v['sd_sfzd']==1?'是':'否';
            $v['sd_sfgdxs'] = $v['sd_sfgdxs']==1?'是':'否';
            $v['sd_sfbtjhs'] = $v['sd_sfbtjhs']==1?'是':'否';
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        return $this->fetch();
    }
    //发布公告
    public function commonInfoNote(){
        if($_POST){
            $data = [];
            $data['title'] = $this->request->post('title');
            $data['content'] = $this->request->post('content');
            $data['fbr'] = session('employee_id');
            $data['fbr_name'] = db('employee')->where('id',session('employee_id'))->value('em_name');
            $data['fbrq'] = date('Y-m-d H:i:s');
            $data['sd_sfzd'] = $this->request->post('sd_sfzd');
            $data['sd_sfgdxs'] = $this->request->post('sd_sfgdxs');
            $data['sd_sfbtjhs'] = $this->request->post('sd_sfbtjhs');
            $data['sd_yxrq'] = $this->request->post('sd_yxrq');
            $valid = new Valid([
               'title|标题'=>'require',
               'content|内容'=>'require',
               'sd_sfzd|是否置顶'=>'require',
               'sd_sfgdxs|是否滚动显示'=>'require',
               'sd_sfbtjhs|是否标题加红色'=>'require',
               'sd_yxrq|以上设定的有效日期'=>'require',
            ]);
            $bool = $valid->check($data);
            if($bool==false){
                echo json_encode(['success'=>0,'msg'=>$valid->getError()]);
                die;
            }
            $attach = $this->request->post('attach');
            if($attach){
                $data['attach'] = $attach;
            }
            $bool = db('cg_ggxx')->insertGetId($data);
            if(false!==$bool){
                if($attach){
                    db('annex')->where('id','in',$attach)->update([
                        'target_table'=>'cg_ggxx',
                        'target_id'=>$bool,
                    ]);
                }
                echo json_encode(['success'=>1,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>0,'msg'=>'error']);
            }
            die;
        }
        $fbr = db('employee')->where('id',session('employee_id'))->value('em_name');
        $this->assign('fbr',$fbr);
        $lx = db('cg_ggxx_lx')->select();
        $this->assign('lx',$lx);
        return $this->fetch();
    }
    //修改公告
    public function update(){
        $id = $this->request->param('id');
        if($_POST){
            $data = [];
            $data['title'] = $this->request->post('title');
            $data['content'] = $this->request->post('content');
            $data['fbr'] = session('employee_id');
            $data['fbr_name'] = db('employee')->where('id',session('employee_id'))->value('em_name');
            $data['fbrq'] = date('Y-m-d H:i:s');
            $data['sd_sfzd'] = $this->request->post('sd_sfzd');
            $data['sd_sfgdxs'] = $this->request->post('sd_sfgdxs');
            $data['sd_sfbtjhs'] = $this->request->post('sd_sfbtjhs');
            $data['sd_yxrq'] = $this->request->post('sd_yxrq');
            $valid = new Valid([
               'title|标题'=>'require',
               'content|内容'=>'require',
               'sd_sfzd|是否置顶'=>'require',
               'sd_sfgdxs|是否滚动显示'=>'require',
               'sd_sfbtjhs|是否标题加红色'=>'require',
               'sd_yxrq|以上设定的有效日期'=>'require',
            ]);
            $bool = $valid->check($data);
            if($bool==false){
                echo json_encode(['success'=>0,'msg'=>$valid->getError()]);
                die;
            }

            $bool = db('cg_ggxx')->where('id',$id)->update($data);
            if(false!==$bool){
                $this->updateAttach();
                echo json_encode(['success'=>1,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>0,'msg'=>'error']);
            }
            die;
        }
        $fbr = db('employee')->where('id',session('employee_id'))->value('em_name');
        $this->assign('fbr',$fbr);
        $lx = db('cg_ggxx_lx')->select();
        $this->assign('lx',$lx);
        $data = db('cg_ggxx')->where('id',$id)->find();
        $this->assign('data',$data);
        //查询附件
        $attach = $data['attach'];
        if($attach){
            $attach = unserialize($attach);

            foreach($attach as $k=>$v){
                $temp = db('annex')->where(array('id'=>$v))->find();

                $temp['path'] = '/uploads/'.$temp['path'];

                $attach[$k] = $temp;
            }
        }

        $this->assign('attach',$attach);
        return $this->fetch();
    }
    /**
     * 修改附件
     * @return mixed
     */
    public function updateAttach(){
        $attach = $this->request->post('attach');
        $id = $this->request->param('id');
        $attach = trim($attach,',');
        $attachids = $attach;

        //先把旧的附件查出来
        $aids = db('cg_ggxx')->where('id',$id)->value('attach');
        $old = $aids;
        if($aids){
            $aids = unserialize($aids);
        }
        if(!is_array($aids)){
            $aids = [];
        }

        //删除附件
        $remove = $this->request->post('remove');
        if($remove){

            $aids = array_diff($aids,explode(',',$remove));
            db('annex')->where('id','in',$remove)->update(['isdel'=>1]);
//                echo Db::table('annex')->getLastSql();
        }

        //新增附件
        if($attach) {
            $attachids = $attach;
            //新旧合并
            $attach = serialize(array_merge(explode(',', $attach), $aids));
        }else { //非新增
            $attach = serialize($aids);
        }

        $data = [];
        $data['attach'] = $attach;

        db('cg_ggxx')->where(array('id'=>$id))->update($data);

        //新增附件\更新附件表
        if(isset($attachids)){

            db('annex')->where(array('id'=>array('in',$attachids)))->update(array('target_id'=>$id,'target_table'=>'cg_ggxx'));
        }


    }

    //新闻公告查询
    public function newsbulletinquery(){
        $title = $this->request->param('title');
        $lx = $this->request->param('lx');
        $fbrq_start = $this->request->param('fbrq_start');
        $fbrq_end = $this->request->param('fbrq_end');
        $fbr = $this->request->param('fbr');
        $this->assign([
            'title'=>$title,
            'lx'=>$lx,
            'fbrq_start'=>$fbrq_start,
            'fbrq_end'=>$fbrq_end,
            'fbr'=>$fbr,
        ]);
        $where = [];
        if($title){
            $where['title'] = ['like','%'.$title.'%'];
        }
        if($lx){
            $where['lx'] = $lx;
        }
        if($fbrq_start&&!$fbrq_end){
            $where['fbrq'] = ['>',$fbrq_start];
        }elseif(!$fbrq_start&&$fbrq_end){
            $where['fbrq'] = ['<',$fbrq_end];
        }elseif($fbrq_start&&$fbrq_end){
            $where['fbrq'] = [['>',$fbrq_start],['<',$fbrq_end],'and'];
        }
        if($fbr){
            $where['fbr_name'] = ['like','%'.$fbr.'%'];
        }
        $count = db('cg_ggxx')->where($where)->count();
        $data = db('cg_ggxx')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['lx'] = db('cg_ggxx_lx')->where('id',$v['lx'])->value('title');
            $v['sd_sfzd'] = $v['sd_sfzd']==1?'是':'否';
            $v['sd_sfgdxs'] = $v['sd_sfgdxs']==1?'是':'否';
            $v['sd_sfbtjhs'] = $v['sd_sfbtjhs']==1?'是':'否';
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        return $this->fetch();
    }
    //删除公告
    public function delete(){
        $id = $this->request->param('id');
        $bool = db('cg_ggxx')->where('id',$id)->delete();
        if(false !== $bool){
            echo json_encode(['success'=>1,'msg'=>'ok']);die;
        }else{
            echo json_encode(['success'=>0,'msg'=>'error']);die;
        }

    }
}